<!-- 修改密码 -->
<template>
  <div class="main-wrapper">
    <el-form ref="passwordForm" :model="ruleForm" status-icon :rules="rules" label-width="100px" class="change-form">
      <el-form-item label="原密码" prop="password">
        <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="新密码" prop="newPass">
        <el-input v-model="ruleForm.newPass" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('passwordForm')">提交</el-button>
        <el-button @click="resetForm('passwordForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { chgPassword } from '@/api/admin'
import getters from '@/store'
import md5 from 'js-md5'

export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入原密码'))
      } else {
        callback()
      }
    }
    const validateNewPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新的密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.passwordForm.validateField('checkPass')
        }
        callback()
      }
    }
    const validateCheckPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新的密码'))
      } else if (value !== this.ruleForm.newPass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      userName: '',
      ruleForm: {
        password: '',
        newPass: '',
        checkPass: ''
      },
      rules: {
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        newPass: [
          { validator: validateNewPass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validateCheckPass, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.userName = getters.name
  },
  methods: {
    submitForm() {
      console.log(getters)
      this.$refs.passwordForm.validate(valid => {
        if (valid) {
          chgPassword(md5(this.ruleForm.password), md5(this.ruleForm.newPass.trim())).then(response => {
            console.log(response)
            // 调用全局挂载的方法
            this.$store.dispatch('tagsView/delView', this.$route)
            // 返回上一步路由
            this.$router.go(-1)
            this.$notify({
              title: '操作成功',
              message: response.message,
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
.main-wrapper {
  margin: 10px;
  text-align: center;
}
.change-form {
  width: 400px;
}
</style>
